package com.wuwei.elearning.question.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.elearning.utils.result.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuwei.elearning.question.domain.SpecialCare;
import com.wuwei.elearning.question.service.SpecialCareService;

import java.util.List;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuwei
 * @since 2025-06-20
 */
@CrossOrigin
@RestController
@RequestMapping("/elearning/specialCare")
public class SpecialCareController {

    @Autowired
    private SpecialCareService specialCareService;

    /**
     * 新增数据
     *
     * @param specialCare 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result<String> save(@RequestBody SpecialCare specialCare) {
        specialCareService.save(specialCare);
        return Result.ok("新增成功");
    }

    /**
     * 根据id删除数据
     *
     * @param id ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<String> removeById(@PathVariable Integer id) {
        specialCareService.removeById(id);
        return Result.ok("删除成功") ;
    }

    /**
     * 根据id修改数据
     *
     * @param specialCare 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result<String> updateById(@RequestBody SpecialCare specialCare) {
        specialCareService.updateById(specialCare);
        return Result.ok("更新成功");
    }

    /**
     * 根据id查询数据
     *
     * @param id ID
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<SpecialCare> getById(@PathVariable Integer id) {
        SpecialCare entity = specialCareService.getById(id);
        return entity != null ? Result.ok(entity) : Result.fail(null);
    }

    /**
     * 分页查询数据
     *
     * @param current 当前页
     * @param size 每页显示条数
     * @return 分页查询结果
     */
    @GetMapping("/page")
    public Result<Page<SpecialCare>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<SpecialCare> page = specialCareService.page(new Page<>(current, size));
        return Result.ok(page);
    }

    /**
     * 查询所有数据
     *
     * @return 查询结果列表
     */
    @GetMapping("/list")
    public Result<List<SpecialCare>> list() {
        List<SpecialCare> list = specialCareService.list();
        return Result.ok(list);
    }

    /**
     * 查询所有数据
     *
     * @return 查询结果列表
     */
    @GetMapping("/getBySubjectId/{subjectId}")
    public Result<List<SpecialCare>> getBySubjectId(@PathVariable("subjectId") Long subjectId) {
        List<SpecialCare> list = specialCareService.list(new LambdaQueryWrapper<SpecialCare>().eq(SpecialCare::getSubjectId,subjectId));
        return Result.ok(list);
    }
}